const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const multer = require("multer");
const upload = multer({ dest: "uploads/" });
const authRoutes = require("./routes/authRoutes");
const cityRoutes = require("./routes/cityRoutes");
const spotRoutes = require("./routes/spotRoutes");
const uploadRoutes = require("./routes/uploadRoutes");
const commentRoutes = require("./routes/commentRoutes");
const sightRoutes = require("./routes/sightRoutes");
const { getHotSpots, getCollection } = require("./controllers/spotController");
const { authenticateToken } = require("./middlewares/authMiddleware");
const app = express();
const PORT = process.env.PORT || 3000;

app.use(cors());
app.use(express.static("uploads"));
app.use(bodyParser.json());

app.use("/api/auth", authRoutes);
app.use("/api/cities", cityRoutes);
app.use("/api/cities/:cityId/spots", spotRoutes);
app.use("/api/upload", upload.single("file"), uploadRoutes);
app.use("/api/comment", commentRoutes);
app.use("/api/spots/new", getHotSpots);
app.use("/api/sights", sightRoutes);
app.use("/api/collcetion", authenticateToken, getCollection);
// Error handler middleware
app.use((err, req, res, next) => {
  console.log(123);
  console.error(err.stack);
  res.status(500).send("An error occurred");
});

app.listen(PORT, () => console.log(`Server running at port ${PORT}`));
